﻿var MultiSel = function( TableName ){
    this.Container = $("#"+TableName);
    this.Source = $("td[class='source'] select",this.Container)[0];
    this.Target = $("td[class='target'] select",this.Container)[0];
    this.AddButton = $("input[name='Add']",$("td[class='button']",this.Container));
    this.AddAllButton = $("input[name='AddAll']",$("td[class='button']",this.Container));
    this.RemoveButton = $("input[name='Remove']",$("td[class='button']",this.Container));
    this.RemoveAllButton = $("input[name='RemoveAll']",$("td[class='button']",this.Container));
    this.UpButton = $("input[name='Up']",$("td[class='button']",this.Container));
    this.DownButton = $("input[name='Down']",$("td[class='button']",this.Container));
    
    this.Capture();
}

MultiSel.prototype.Add = function( sValue ){
    for( var i = 0; i < this.Source.length; i++ ){
        if( this.Source.options[i].value == sValue ){
            this.Target.options.add(new Option(this.Source.options[i].text,this.Source.options[i].value));
            this.Source.options[i] = null;
            break;
        }
    }
}

MultiSel.prototype.Capture = function(){
    var oThis = this;
    this.AddButton.click( function(){
        for( var i = oThis.Source.length - 1; i >= 0; i-- ){
            if( oThis.Source.options[i].selected ){
                oThis.Target.options.add(new Option(oThis.Source.options[i].text,oThis.Source.options[i].value));
                oThis.Source.options[i] = null;
            }
        }
    });
    
    this.RemoveButton.click( function(){
        for( var i = oThis.Target.length - 1; i >= 0; i-- ){
            if( oThis.Target.options[i].selected ){
                oThis.Source.options.add(new Option(oThis.Target.options[i].text,oThis.Target.options[i].value));
                oThis.Target.options[i] = null;
            }
        }
    });
    
    this.AddAllButton.click( function(){
        for( var i = oThis.Source.length - 1; i >= 0; i-- ){
            oThis.Target.options.add(new Option(oThis.Source.options[i].text,oThis.Source.options[i].value));
            oThis.Source.options[i] = null;
        }
    });
    
    this.RemoveAllButton.click( function(){
        for( var i = oThis.Target.length - 1; i >= 0; i-- ){
            oThis.Source.options.add(new Option(oThis.Target.options[i].text,oThis.Target.options[i].value));
            oThis.Target.options[i] = null;
        }
    });
    
    this.UpButton.click( function(){
        var nIndex = oThis.Target.selectedIndex;
        if( nIndex > 0 ){
            var obj = oThis.Target.options[nIndex-1];
            oThis.Target.options[nIndex-1] = new Option(oThis.Target.options[nIndex].text,oThis.Target.options[nIndex].value);
            oThis.Target.options[nIndex] = new Option(obj.text,obj.value);
            oThis.Target.selectedIndex = nIndex - 1;
        }
    });
    
    this.DownButton.click( function(){
        var nIndex = oThis.Target.selectedIndex;
        if( nIndex < oThis.Target.length - 1 ){
            var obj = oThis.Target.options[nIndex+1];
            oThis.Target.options[nIndex+1] = new Option(oThis.Target.options[nIndex].text,oThis.Target.options[nIndex].value);
            oThis.Target.options[nIndex] = new Option(obj.text,obj.value);
            oThis.Target.selectedIndex = nIndex + 1;
        }
    });
    
    $(this.Source).dblclick( function(){
        oThis.AddButton.click();
    });
    
    $(this.Target).dblclick( function(){
        oThis.RemoveButton.click();
    });
}